#include <stdlib.h>
#include "B3_defines.h"
#include "playnote.h"

extern int key2wheel [KEY_NUM + 1][DRAWBAR_NUM + 1];

extern long wheel_v_current[WHEEL_NUM + 1];
extern long wheel_v_dest[WHEEL_NUM + 1];
extern keys_t keys1[KEY_NUM + 1];
extern keys_t keys2[KEY_NUM + 1];
extern drawbar_t drawbar1[DRAWBAR_NUM + 1];
extern drawbar_t drawbar2[DRAWBAR_NUM + 1];
extern drawbar_level_t drawbar_level[DRAWBAR_LEVEL_NUM];

void
calculate_wheel_v() {
        int i, j;

        //new_v was designed for the realtime version dodorgan. not needed in offline version.
        long *new_v = (long*) malloc((WHEEL_NUM + 1) * sizeof(long));

        for (i = 1; i <= WHEEL_NUM; ++i)
                new_v[i] = 0;

        for (i = 1; i <= KEY_NUM; ++i) {
                for (j = 1; j <= DRAWBAR_NUM; ++j) {
                        new_v[ key2wheel[i][j] ] +=
                                (keys1[i] * drawbar_level[drawbar1[j]] + keys2[i] * drawbar_level[drawbar2[j]]);
                }
        }

        for (i = 1; i <= WHEEL_NUM; ++i) {
                wheel_v_dest[i] = new_v[i];
        }

        free(new_v);
}
